﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;

namespace SadlandVillage.Core
{
    public class GameStateManager
    {
        private Dictionary<MiniGameType,MiniGame> minigames;
        private MiniGame activeMinigame;
        private Menu mainMenu;
        private Menu pauseMenu;
        private GameScene map;
        private GameScene credits;
        private GameState gameState;
        private GameScene calibration;

        public GameStateManager(Dictionary<MiniGameType, MiniGame> minigames, Menu mainMenu, Menu pauseMenu, GameScene map, GameScene credits, GameScene calibration)
        {
            this.mainMenu = mainMenu;
            this.pauseMenu = pauseMenu;
            this.minigames = minigames;
            this.credits = credits;
            activeMinigame = minigames.First().Value;
            gameState = GameState.Menu;
            this.map = map;
            this.calibration = calibration;
        }

        public void SetActiveGame(MiniGameType miniGameType)
        {
            activeMinigame.Active = false;
            minigames[miniGameType].Active = true;
            activeMinigame = minigames[miniGameType];
            SetCurrentState(GameState.MinigameGameplay);
        }

        public void SetCurrentState(GameState state)
        {
            if (ShouldStopMediaPlayer(state))
                MediaPlayer.Stop();

            this.gameState = state;
        }

        private bool ShouldStopMediaPlayer(GameState state)
        {
            if (state == GameState.Menu || state == GameState.Paused &&
                gameState == GameState.Menu || gameState == GameState.Paused)
                return false;
            else if (state == GameState.Credits)
                return false;
            else
                return true;
        }

        public void Update(GameTime gameTime)
        {
            switch (gameState)
            {
                case GameState.Menu:
                    mainMenu.Update(gameTime);
                    break;
                case GameState.MapGameplay:
                    map.Update(gameTime);
                    break;
                case GameState.MinigameGameplay:
                    activeMinigame.Update(gameTime);
                    break;
                case GameState.Paused:
                    pauseMenu.Update(gameTime);
                    break;
                case GameState.GameOver:
                    break;
                case GameState.Credits:
                    credits.Update(gameTime);
                    break;
                case GameState.Calibration:
                    calibration.Update(gameTime);
                    break;
                default:
                    break;
            }
        }

        public void Draw(GameTime gameTime)
        {
            switch (gameState)
            {
                case GameState.Menu:
                    mainMenu.Draw(gameTime);
                    break;
                case GameState.MapGameplay:
                    map.Draw(gameTime);
                    break;
                case GameState.MinigameGameplay:
                    activeMinigame.Draw(gameTime);
                    break;
                case GameState.Paused:
                    map.Draw(gameTime);
                    pauseMenu.Draw(gameTime);
                    break;
                case GameState.GameOver:
                    break;
                case GameState.Credits:
                    credits.Draw(gameTime);
                    break;
                case GameState.Calibration:
                    calibration.Draw(gameTime);
                    break;
                default:
                    break;
            }
        }

    }
}
